<?php 
	class Code
	{	
		public $configs = array(
		'width'=> 200,
		'height'=> 100,
		'length' => 4,
		'line' => 10,
		'point' => 1000,
		'size' => 80,
		'str' => "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890",
			);
		
		public $src;

		function __get($key)
		{
			return $this->configs[$key];
		}
		function __construct($configs=array())
		{	
			$this->configs=array_merge($this->configs,$configs);
		}
		//用此方法生成验证码
		public function show()
		{	
			header("Content-type:image/png");
			//创建画布
			$this->huabu();
			//画布加入字体
			$this->world();
			//画线条
			$this->line();
			//画点
			$this->point();
			//显示验证码;
			imagepng($this->src);

		}

		private function huabu()
		{
			$this->src = imageCreateTrueColor($this->width,$this->height); 
			$color = imagecolorallocate($this->src, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
			//填充颜色
			imagefill($this->src, 0, 0, $color);
		}
     	//画布上画直线
		private function line()
		{	

			for ($i=0; $i <$this->line ; $i++) { 
				$color = imagecolorallocate($this->src, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
				imageline ($this->src, mt_rand(0,$this->width), 0 ,mt_rand(0,$this->width), $this->height, $color );
			}
		}
		private function point()
		{
			//画点
			for ($i=0; $i < $this->point; $i++) { 
				$color = imagecolorallocate($this->src, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
				imagesetpixel($this->src, mt_rand(0,$this->width), mt_rand(0,$this->height), $color);
			}
		}
		//在画布上加入字体
		
		private function world()
		{
			for ($i=0; $i < $this->length; $i++) { 
				$str = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
				//循环$length次 随机取
				$rand = mt_rand(0,strlen($str)-1);
				$code = $str[$rand];
				$color = imagecolorallocate($this->src, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
				$x = $i*(($this->width)/4-10);
				$x+=10;
				$y = $this->height*3/4;
				imagettftext($this->src, $this->size, mt_rand(-30,30), $x, $y, $color, './HalfTones.ttf', $code);
			}
		}
	
	}
	//验证码配置
	$configs = array(
		'width'=> 200,
		'height'=> 100,
		'length' => 4,
		'line' => 10,
		'point' => 1000,
		'size' => 80,
		);
	$code = new Code();
	$code->show();


























 ?>